package com.thinkgem.jeesite.modules.self.web;

import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.book.service.BookStoreService;
import com.thinkgem.jeesite.modules.borrow.entity.Borrow;
import com.thinkgem.jeesite.modules.borrow.entity.BorrowLog;
import com.thinkgem.jeesite.modules.borrow.service.BorrowService;
import com.thinkgem.jeesite.modules.reader.entity.Reader;
import com.thinkgem.jeesite.modules.reader.entity.ReaderRoler;
import com.thinkgem.jeesite.modules.reader.service.ReadersService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author John
 * @Date 2016/10/15
 */
@Controller
public class FrontSelfController extends BaseController{

    @Autowired
    private ReadersService readerService;
    @Autowired
    private BorrowService borrowService;
    @Autowired
    private BookStoreService bookStoreService;

    /**
     * 自助服务首页
     */
    @RequestMapping(value = "")
    public String mian(){
        return "modules/main";
    }

    /**
     * 自助服务首页
     */
    @RequestMapping("welcome")
    public String index(HttpSession session){
        session.invalidate();
        return "modules/welcome";
    }

    /**
     * 自助服务首页
     */
    @RequestMapping("borrow")
    public String brrow(Model model, HttpSession session){
        Reader reader = (Reader)session.getAttribute("reader");
        List<Borrow> borrows = borrowService.findBorrowListbyRdid(reader.getRdId());
        model.addAttribute("borrows", borrows);

        model.addAttribute("now_date", DateUtils.getDate());
        return "modules/borrow/borrow";
    }

    /**
     * 借书
     * @param banid
     * @param session
     * @return
     */
    @RequestMapping("borrow_book")
    @ResponseBody
    public Result borrow_book(String banid, HttpSession session) throws ParseException{
        Reader reader = (Reader)session.getAttribute("reader");
        return borrowService.borrow_book(banid, reader, "self");
    }

    /**
     * 自助还书服务首页
     */
    @RequestMapping("return")
    public String returnBk(){
        return "modules/return/return";
    }

    /**
     * 跳转到续借首页
     */
    @RequestMapping("return_book")
    @ResponseBody
    public Result return_book(String banid){
        return borrowService.return_book(banid);
    }

    /**
     * 跳转到续借首页
     */
    @RequestMapping("renew")
    public String renew(Model model, HttpSession session){
        Reader reader = (Reader)session.getAttribute("reader");
        List<Borrow> borrows = borrowService.findBorrowListbyRdid(reader.getRdId());
        model.addAttribute("borrows", borrows);

        return "modules/renew/renew";
    }

    /**
     * 跳转到续借首页
     */
    @RequestMapping("renew_book")
    @ResponseBody
    public Result renew_book(String zlId, HttpSession session) throws ParseException{
        Reader reader = (Reader)session.getAttribute("reader");

        //根据借阅记录号，查询这条记录
        Borrow borrow= borrowService.findOne(zlId);
        if(null != borrow){
            String bookname = borrow.getBkBookinfo().getBookname();
            ReaderRoler readerRoler = readerService.findReaderRoleByRdid(reader);

            //根据读者编号，查询读者角色编号，根据角色编号，查询角色可以续借的本书，天数等等
            String rdContinueBorrowCount = readerRoler.getRdContinueBorrowCount();//可续借次数
            String rdContinueBorrowDays = readerRoler.getRdContinueBorrowDays();//可续借天数
            //2.续借的开始日期  假如30号到期，25号续借的，续借是从30号算
            SimpleDateFormat sdf11 = new SimpleDateFormat("yyyy-MM-dd");
            Date date = borrow.getNeedRtnDate();
            String str = sdf11.format(date);
            String continurtnday = DateUtils.dateadd(str, Integer.valueOf(rdContinueBorrowDays));//续借的开始日期=上次借出的日期+可以续借的天数
            int count = Integer.valueOf(borrow.getContCnt()) + 1;//续借次数
            //需要判断续借的次数和本书要满足续借的条件，即不能大于满足的条件
            if(Integer.valueOf(rdContinueBorrowCount) < count){
                return new Result(Result.FAILURE, "《" + bookname + "》已超出续借次数！", null);
            }
            else{
                //满足条件即可续借  续借次数为0插入  续借次数>0 更新
                if(Integer.valueOf(borrow.getContCnt()) == 0){
                    borrowService.savebrcontinue(borrow.getZlId(), str, continurtnday, UserUtils.getUser().getLoginName(), "1", count);
                }
                else{
                    borrowService.updateCont(borrow.getZlId(), str, continurtnday, UserUtils.getUser().getLoginName(), "1", count);
                }
                //日志记录
                BorrowLog borrowLog = new BorrowLog("2", borrow.getRdId(), readerService.getUser(borrow.getRdId()).getRdName(), "书籍",
                        borrow.getBkId(), borrowService.makeBookName(borrow.getBkId()), "0", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
                        borrow.getZlId(), bookStoreService.get(borrow.getBkId()).getBkId(), UserUtils.getUser().getLoginName());
                borrowService.saveLog(borrowLog);
                return new Result(Result.SUCCESS, "续借《" + bookname + "》成功！", continurtnday);
            }
        }
        else{
            return new Result(Result.FAILURE, "没有查询到该借阅信息,请联系管理员！", null);
        }
    }
}
